﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UITesting.WpfControls;

namespace CodedUITestExtensions
{
    /// <summary>
    /// Extensions for WpfTreeItem.
    /// </summary>
    public static class WpfTreeItemExtensions
    {
        /// <summary>
        /// Gets WpfTreeItem text when it's not displayed in Name property.
        /// </summary>
        /// <param name="treeItem">WpfTreeItem.</param>
        /// <returns>WpfTreeItem text.</returns>
        public static string GetTreeItemText(this WpfTreeItem treeItem)
        {
            if (treeItem == null)
            {
                throw new ArgumentNullException("treeItem");
            }
            var wpfText = new WpfText(treeItem);
            var wpfTextCollection = wpfText.FindMatchingControls().Cast<WpfText>();
            IEnumerable<string> texts = wpfTextCollection.Select(control => control.DisplayText);
            string actualText = string.Join("", texts);
            return actualText;
        }
    }
}
